package ro.qi.framework.sys.repository;

import org.w3c.dom.NodeList;

import ro.qi.framework.sys.debug.Logger;
import ro.qi.framework.sys.entities.components.EntityComponent;
import ro.qi.framework.sys.entities.exception.ComponentException;

/**
 * @author Denverash
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ComponentConfiguration {

	public static Object locker = new Object();

	private String name = "";
	private String className = "";
	private String defaultValue = "";
	private String fieldName = "";
	private NodeList configuration = null;

	public Object getComponentInstance() {
		try {
			Object obj = Class.forName(className).newInstance();
			if( obj instanceof EntityComponent ) {
				if( defaultValue.length() > 0 ) {
					try {
						((EntityComponent)obj).setValue(defaultValue);
					} catch (ComponentException e1) {
						e1.printStackTrace();
					}
				}
				if( configuration != null ) {
					synchronized (locker) {
						((EntityComponent)obj).setConfig(configuration);
					}
				}
			}
			return obj;
		} catch (InstantiationException e) {
			Logger.error(this, "Instantiation Exception: " + e.getMessage());
		} catch (IllegalAccessException e) {
			Logger.error(this, "Illegal Access Exception: " + e.getMessage());
		} catch (ClassNotFoundException e) {
			Logger.error(this, "Class Not Found Exception: " + e.getMessage());
		}
		return null;
	}

	/**
	 * @return
	 */
	public String getClassName() {
		return className;
	}

	/**
	 * @return
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	/**
	 * @return
	 */
	public String getFieldName() {
		return fieldName;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param string
	 */
	public void setClassName(String string) {
		className = string;
	}

	/**
	 * @param string
	 */
	public void setDefaultValue(String string) {
		defaultValue = string;
	}

	/**
	 * @param string
	 */
	public void setFieldName(String string) {
		fieldName = string;
	}

	/**
	 * @param string
	 */
	public void setName(String string) {
		name = string;
	}

	/**
	 * @param list
	 */
	public void setConfiguration(NodeList list) {
		configuration = list;
	}

	/**
	 * @return
	 */
	public NodeList getConfiguration() {
		return configuration;
	}

	public String toString() {
		return name + " [" + className + ";" + defaultValue + ";" + fieldName + "]";
	}

}
